package com.cares.event.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("unused")
public class DateUtil {
	
	public final static String DATE_FORMAT_YMD = "yyyy-MM-dd";
	public final static String DATE_FORMAT_YM = "yyyy-MM";
	public final static String DATE_FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
	

	private final static String DATE_FORMATE_SHORT = "yyyyMMdd";
	public final static String DATE_FORMATE_FULL = "yyyyMMddHHmm";
	private final static String DATEFORMATE_ALL = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 取得日期字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateStr(Date date, String format) {
		if(date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		}
		return "";
	}
	/**
	 * 取得字符串日期
	 * @param str
	 * @param format
	 */
	public static Date getStrDate(String str ,String format) throws ParseException{
		if(str!=null){
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(str);
		}
		return null;
	}
	
	/**
	 * 
	 * 计算两个日期相隔的分钟数
	 * @param dt1,dt2
	 * @return
	 * @throws ParseException 
	 */
	public static int calcTwoDatesMinut(Date dt1, Date dt2){
		Long  days = 0l;
		try{
			//只取年月日
			if(dt1!=null && dt2!=null){
				SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_LONG);
				String strDt1 = sdf.format(dt1);
				Date temp1 = sdf.parse(strDt1);
				String strDt2 = sdf.format(dt2);
				Date temp2 = sdf.parse(strDt2);
				days = (temp2.getTime()-temp1.getTime())/(1000*60); 
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days.intValue();
	} 
	/**
	 * 取得当前月第一天
	 * @param date
	 * @return
	 */
	public static Date getMonthFirst(Date date) {
		try {
			SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_YM);
			SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
			SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
			return sdfShort.parse(sdfYear.format(date) + "-" + sdfMonth.format(date) + "-01");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 取得当前月最后一天
	 * @param date
	 * @return
	 */
	public static Date getMonthLast(Date date) {
		try {
			SimpleDateFormat sdfShort = new SimpleDateFormat(DATE_FORMAT_YM);
			SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
			SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
			int year = Integer.valueOf(sdfYear.format(date));
			int month = Integer.valueOf(sdfMonth.format(date));
			int day = getMonthDays(year, month);
			return sdfShort.parse(year + "-" + month + "-" + day);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 取得某月天数
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getMonthDays(int year, int month) {
		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);//把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}
	
}
